'use strict';
/**
 * Created by QianQi on 2017/3/9.
 */
$(function(){
    var oHideParams = $.getHideParams(),// <body> 下隐藏域键值对，name 为键
        oUrlParams = $.getUrlParams();// url 附加参数键值对
    var URLS = {
        d_tree: 'demo0.json'// 树数据源
    };
    var DATA = {
        tree: [
            {
                "groupname": "系统字典",
                "id": "001"
            }, {
                "groupname": "用户字典",
                "id": "002"
            }, {
                "groupname": "管理员",
                "id": "003"
            }
        ],
        staticTree: [
            {
                "groupname": "静态数据0",
                "id": "001"
            }, {
                "groupname": "静态数据1",
                "id": "002"
            }, {
                "groupname": "管理员",
                "id": "003"
            }
        ]
    };
    var jqTree = $('.js-tree');
    var oZTreeApi;// 树组件 api

    // 顶部按钮点击事件
    var fTbtnsClick = function(){
        var jqThis = $(this);
        switch(jqThis.attr('data-type')){
            case 'resetData':// 切换树数据源
                oZTreeApi.reset(DATA.staticTree);
                break;
            case 'resetUrl':// 重新请求数据
                oZTreeApi.reset(URLS.d_tree, undefined, ['groupname', '系统管理员']);
                break;
            case 'select':// 选中节点
                oZTreeApi.setSel(['groupname', '管理员']);
//                oZTreeApi.reset(undefined,undefined,['groupname',['管理员','九江县']]);
                break;
            case 'getSelectedNodes':
                console.log('取值', oZTreeApi.getSelectedNodes());
                break;
        }
    };

    // zTree 组件初始化完成的回调
    var fAfterInit = function(ztree){
        console.log('afterinit');
    };

    // 修改选中项后的回调
    var fOnSelect = function(selNodes, treeObj){
        console.log('onSelect', selNodes);
    };

    // 树节点点击事件
    var fOnTreeNodeClick = function(event, treeId, node){
        console.log('点击了节点:', node);
    };

    // 树节点勾选改变事件
    var fOnTreeNodeCheck = function(event, treeId, treeNode){
        console.log('check', treeNode['groupname']);
    };
    /** =================== **/
    $('.js-tbtns').on('click', 'button', fTbtnsClick);// 顶部按钮点击事件
    oZTreeApi = jqTree.initZTree({
        data: DATA.tree,
        //selParams: ['groupname','系统管理员'],// 初始化时点击符合条件的第一个节点
        //multi: true,
        //selector:function(node){// 可选中的节点返回 true
        //    return node['level']>0;
        //},
        //check: {
        //    enable: true
        //},
        //initSelect:false,
        key: {
            //checked:'checked',// 标识是否选中的字段
            //children:'children',// 子节点集合字段
            name: 'groupname'// 显示字段
        },
        afterInit: fAfterInit,
        onSelect: fOnSelect,
        callback: {
            onClick: fOnTreeNodeClick,
            onCheck: fOnTreeNodeCheck
        }
    });
});